import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { authApi } from '@/api/auth'

export const useAuthStore = defineStore('auth', () => {
  const token = ref(localStorage.getItem('token') || '')
  const user = ref(JSON.parse(localStorage.getItem('user') || 'null'))

  const isLoggedIn = computed(() => !!token.value && !!user.value)

  const login = async (credentials) => {
    try {
      const response = await authApi.login(credentials)
      const { token: newToken, user: userData } = response.data.data
      
      token.value = newToken
      user.value = userData
      
      localStorage.setItem('token', newToken)
      localStorage.setItem('user', JSON.stringify(userData))
      
      return { success: true }
    } catch (error) {
      return { 
        success: false, 
        message: error.response?.data?.message || '登录失败' 
      }
    }
  }

  const register = async (userData) => {
    try {
      const response = await authApi.register(userData)
      const { token: newToken, user: userInfo } = response.data.data
      
      token.value = newToken
      user.value = userInfo
      
      localStorage.setItem('token', newToken)
      localStorage.setItem('user', JSON.stringify(userInfo))
      
      return { success: true }
    } catch (error) {
      return { 
        success: false, 
        message: error.response?.data?.message || '注册失败' 
      }
    }
  }

  const logout = async () => {
    if (user.value && user.value.username) {
      try {
        await authApi.logout(user.value.username)
      } catch (error) {
        console.error('后端登出失败:', error)
      }
    }
    token.value = ''
    user.value = null
    localStorage.removeItem('token')
    localStorage.removeItem('user')
  }

  return {
    token,
    user,
    isLoggedIn,
    login,
    register,
    logout
  }
}) 